package ui.activity.mine.presenter;


import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import Bean.GetBankcardBean;
import base.BaseBiz;
import model.PersonalCenterBean;
import model.PersonalCenterParamBean;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.mine.PayPwdSuccessAct;
import ui.activity.mine.biz.PayPwdBiz;
import ui.activity.mine.contract.PayPwdContract;
import ui.model.PayPwdBean;
import util.StringUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class PayPwdPresenter implements PayPwdContract.Presenter {

    PayPwdContract.View view;
    PayPwdBiz payPwdBiz;
    List<GetBankcardBean.DataBean.ItemsBean> list = new ArrayList<>();


    @Inject
    public PayPwdPresenter(PayPwdContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        payPwdBiz = (PayPwdBiz) biz;
    }

    @Override
    public void getCode() {
        view.loading(true);
        payPwdBiz.getCode(new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                view.showMsg("发送验证码成功");
                view.upDataCode();
                view.loading(false);
            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void setPwd(boolean isShow) {
        view.upDatePwd(isShow);
    }

    @Override
    public void submitData(String phone, String secret, String validateCode) {

        if (StringUtils.isEmpty(phone)) {
            view.showMsg("请退出当前界面重新提交密码");
            return;
        }
        if (StringUtils.isEmpty(secret)) {
            view.showMsg("请输入6位支付密码");
            return;
        }
        if (StringUtils.isEmpty(validateCode)) {
            view.showMsg("请输入验证码");
            return;
        }
        if (secret.length() != 6) {
            view.showMsg("请输入6位支付密码");
            return;
        }
        view.loading(true);
        PayPwdBean p = new PayPwdBean();
        p.setSecret(secret);
        p.setValidateCode(validateCode);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(p));
        payPwdBiz.setPwd(requestBody, new BaseBiz.Callback<Boolean>() {
            @Override
            public void onSuccess(Boolean info) {
                view.startAct(PayPwdSuccessAct.class, null);
                view.finishAct();
                view.loading(false);
            }

            @Override
            public void onFailure(Boolean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getUserInfo(String id) {
        view.loading(true);
        PersonalCenterParamBean p = new PersonalCenterParamBean();
        p.setCustomId(id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(p));
        payPwdBiz.getUserIncome(requestBody, new BaseBiz.Callback<PersonalCenterBean.DataBean>() {
            @Override
            public void onSuccess(PersonalCenterBean.DataBean info) {
                view.upDateUser(info);
                view.loading(false);
            }

            @Override
            public void onFailure(PersonalCenterBean.DataBean info) {
                view.loading(false);
            }
        });
    }

}
